import mongoose, { Document, Schema } from 'mongoose';
import { IExpense, ExpenseType } from '@/types';

export interface IExpenseDocument extends Omit<IExpense, '_id'>, Document {}

const expenseSchema = new Schema<IExpenseDocument>(
  {
    userId: {
      type: String,
      required: [true, '用户ID是必需的'],
    },
    vehicleId: {
      type: String,
      required: [true, '车辆ID是必需的'],
    },
    type: {
      type: String,
      enum: Object.values(ExpenseType),
      required: [true, '费用类型是必需的'],
    },
    /** 里程数 */
    mileage: {
      type: Number,
      required: [true, '里程数是必需的'],
      min: [0, '里程数不能为负数'],
    },
    /** 加油信息 */
    fuelInfo: {
      showPrice: { type: Number, required: true },
      actualPrice: { type: Number },
      showAmount: { type: Number, required: true },
      fuelAmount: { type: Number, required: true },
      fuelType: {
        type: String,
        enum: ['#92', '#95', '#98', '柴油', '电', '其他'],
      },
      discountAmount: { type: Number },
      actualAmount: { type: Number },
    },
    /** 邮箱是否加满 */
    isFull: {
      type: Boolean,
      default: false,
    },
    /** 上次是否记录 */
    isRecord: {
      type: Boolean,
      default: false,
    },
    /** 油箱灯是否亮 */
    isWarning: {
      type: Boolean,
      default: false,
    },
    /** 货币 */
    currency: {
      type: String,
      enum: ['CNY', 'USD', 'EUR'],
      default: 'CNY',
    },
    date: {
      type: Date,
      required: [true, '费用日期是必需的'],
      default: Date.now,
    },
    /** 备注 */
    notes: {
      type: String,
      trim: true,
      maxlength: [500, '备注不能超过500个字符'],
    },
    /** 费用地点 */
    location: {
      type: String,
      trim: true,
      maxlength: [200, '费用地点不能超过200个字符'],
    },
    /** 里程表（保留向后兼容） */
    odometer: {
      type: Number,
      min: [0, '里程表不能为负数'],
    },
    /** 描述（保留向后兼容） */
    description: {
      type: String,
      trim: true,
      maxlength: [500, '描述不能超过500个字符'],
    },
    imageUrl: {
      type: String,
      trim: true,
    },
  },
  {
    timestamps: true,
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

// 虚拟字段：时间戳
expenseSchema.virtual('createdAtTimestamp').get(function () {
  return this.createdAt ? this.createdAt.getTime() : null;
});

expenseSchema.virtual('updatedAtTimestamp').get(function () {
  return this.updatedAt ? this.updatedAt.getTime() : null;
});

expenseSchema.virtual('dateTimestamp').get(function () {
  return this.date ? this.date.getTime() : null;
});

// 虚拟字段：总金额
expenseSchema.virtual('totalAmount').get(function () {
  if (this.fuelInfo && this.fuelInfo.actualAmount) {
    return this.fuelInfo.actualAmount;
  }
  if (this.fuelInfo && this.fuelInfo.showAmount) {
    return this.fuelInfo.showAmount;
  }
  return 0;
});

// 索引
expenseSchema.index({ userId: 1 });
expenseSchema.index({ vehicleId: 1 });
expenseSchema.index({ type: 1 });
expenseSchema.index({ date: 1 });
expenseSchema.index({ userId: 1, vehicleId: 1 });
expenseSchema.index({ userId: 1, date: 1 });

export const Expense = mongoose.model<IExpenseDocument>(
  'Expense',
  expenseSchema
);
